﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
using System.Text;

namespace GestionNewsLetters.Models.Classes
{
    /// <summary>
    /// Represents a newsletter
    /// </summary>
    public class Newsletter
    {
        /// <summary>
        /// Newsletter ID
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// Newsletter URL.
        /// </summary>
        public string Lien
        {
            get
            {
                return Helper.GetUrlApplication() + (this.Etat == EtatNewsletter.En_attente? "/_Newsletters/" : "/Newsletters/") + this.Id + ".html";
            }
        }

        /// <summary>
        /// Newsletter state publish/pending publication
        /// </summary>
        public EtatNewsletter Etat { get; set; }

        /// <summary>
        /// Newsletter content, including title. HTML formated.
        /// </summary>
        public string Contenu { get; set; }

        /// <summary>
        /// Newsletter title
        /// </summary>
        public string Title
        {
            //We extract newsletter title.
            //On extrait le titre du contenu de la newsletter: il est entre les balises title
            get
            {
                int start = this.Contenu.IndexOf("<title>") + 7;
                int length = this.Contenu.IndexOf("</title>") - start;
                return this.Contenu.Substring(start, length);
            }
        }
    }
    /// <summary>
    /// Represents the state of the newsletter
    /// </summary>
    public enum EtatNewsletter
    {
        /// <summary>
        /// Pending publication
        /// </summary>
        En_attente,
        /// <summary>
        /// Publicated
        /// </summary>
        Envoye
    }
}